package proyectoswing;

import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.JOptionPane;

public class frmCompra extends javax.swing.JFrame {

    Compra compra;
    ArrayList<Cliente>listaCliente;
    /** Creates new form frmCompra */
    public frmCompra() {
        initComponents();
        listaCliente = (ArrayList<Cliente>)Cliente.getListaCliente();
        
        
        for(Cliente c: listaCliente){
           this.cmbCliente.addItem(c);
        }

        compra = new Compra();
        compra.setListaProductos(new ArrayList());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnGroupPago = new javax.swing.ButtonGroup();
        btnGroupEntrega = new javax.swing.ButtonGroup();
        chkB1 = new javax.swing.JCheckBox();
        chkB2 = new javax.swing.JCheckBox();
        chkB3 = new javax.swing.JCheckBox();
        chkB4 = new javax.swing.JCheckBox();
        chkB5 = new javax.swing.JCheckBox();
        chkB6 = new javax.swing.JCheckBox();
        chkB7 = new javax.swing.JCheckBox();
        chkB8 = new javax.swing.JCheckBox();
        chkB9 = new javax.swing.JCheckBox();
        chkB10 = new javax.swing.JCheckBox();
        lbComprar = new javax.swing.JLabel();
        rbEfectivo = new javax.swing.JRadioButton();
        rbTarjeta = new javax.swing.JRadioButton();
        rbTransferencia = new javax.swing.JRadioButton();
        lbSeleccionProducto = new javax.swing.JLabel();
        lbSeleccionPago = new javax.swing.JLabel();
        lbSeleccionEntrega = new javax.swing.JLabel();
        tbDomicilio = new javax.swing.JToggleButton();
        tbRecoger = new javax.swing.JToggleButton();
        btnAceptar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        cmbCliente = new javax.swing.JComboBox();
        lbCliente = new javax.swing.JLabel();

        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        chkB1.setText("producto 1");
        chkB1.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        chkB1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chkB1ActionPerformed(evt);
            }
        });
        getContentPane().add(chkB1, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 150, -1, -1));

        chkB2.setText("producto 2");
        chkB2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        chkB2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chkB2ActionPerformed(evt);
            }
        });
        getContentPane().add(chkB2, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 180, -1, -1));

        chkB3.setText("producto 3");
        chkB3.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        chkB3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chkB3ActionPerformed(evt);
            }
        });
        getContentPane().add(chkB3, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 210, -1, -1));

        chkB4.setText("producto 4");
        chkB4.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        chkB4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chkB4ActionPerformed(evt);
            }
        });
        getContentPane().add(chkB4, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 240, -1, -1));

        chkB5.setText("producto 5");
        chkB5.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        chkB5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chkB5ActionPerformed(evt);
            }
        });
        getContentPane().add(chkB5, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 270, -1, -1));

        chkB6.setText("producto 6");
        chkB6.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        chkB6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chkB6ActionPerformed(evt);
            }
        });
        getContentPane().add(chkB6, new org.netbeans.lib.awtextra.AbsoluteConstraints(310, 150, -1, -1));

        chkB7.setText("producto 7");
        chkB7.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        chkB7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chkB7ActionPerformed(evt);
            }
        });
        getContentPane().add(chkB7, new org.netbeans.lib.awtextra.AbsoluteConstraints(310, 180, -1, -1));

        chkB8.setText("producto 8");
        chkB8.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        chkB8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chkB8ActionPerformed(evt);
            }
        });
        getContentPane().add(chkB8, new org.netbeans.lib.awtextra.AbsoluteConstraints(310, 210, -1, -1));

        chkB9.setText("producto 9");
        chkB9.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        chkB9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chkB9ActionPerformed(evt);
            }
        });
        getContentPane().add(chkB9, new org.netbeans.lib.awtextra.AbsoluteConstraints(310, 240, -1, -1));

        chkB10.setText("producto 10");
        chkB10.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        chkB10.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chkB10ActionPerformed(evt);
            }
        });
        getContentPane().add(chkB10, new org.netbeans.lib.awtextra.AbsoluteConstraints(310, 270, -1, -1));

        lbComprar.setFont(new java.awt.Font("Verdana", 1, 12));
        lbComprar.setText("REALIZAR COMPRA");
        getContentPane().add(lbComprar, new org.netbeans.lib.awtextra.AbsoluteConstraints(270, 40, 136, -1));

        btnGroupPago.add(rbEfectivo);
        rbEfectivo.setText("Efectivo");
        rbEfectivo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbEfectivoActionPerformed(evt);
            }
        });
        getContentPane().add(rbEfectivo, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 350, -1, -1));

        btnGroupPago.add(rbTarjeta);
        rbTarjeta.setText("Tarjeta");
        rbTarjeta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbTarjetaActionPerformed(evt);
            }
        });
        getContentPane().add(rbTarjeta, new org.netbeans.lib.awtextra.AbsoluteConstraints(230, 350, -1, -1));

        btnGroupPago.add(rbTransferencia);
        rbTransferencia.setActionCommand("Transferencia Bancaria");
        rbTransferencia.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        rbTransferencia.setLabel("<html>Transferencia<br/>    Bancaria</html>");
        rbTransferencia.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbTransferenciaActionPerformed(evt);
            }
        });
        getContentPane().add(rbTransferencia, new org.netbeans.lib.awtextra.AbsoluteConstraints(350, 340, 100, -1));

        lbSeleccionProducto.setFont(new java.awt.Font("Tahoma", 1, 11));
        lbSeleccionProducto.setText("Seleccionar los productos");
        getContentPane().add(lbSeleccionProducto, new org.netbeans.lib.awtextra.AbsoluteConstraints(31, 119, -1, -1));

        lbSeleccionPago.setFont(new java.awt.Font("Tahoma", 1, 11));
        lbSeleccionPago.setText("Seleccionar forma de pago");
        getContentPane().add(lbSeleccionPago, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 320, -1, -1));

        lbSeleccionEntrega.setFont(new java.awt.Font("Tahoma", 1, 11));
        lbSeleccionEntrega.setText("Seleccionar Entrega");
        getContentPane().add(lbSeleccionEntrega, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 390, -1, -1));

        btnGroupEntrega.add(tbDomicilio);
        tbDomicilio.setText("Entrega Domicilio");
        tbDomicilio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tbDomicilioActionPerformed(evt);
            }
        });
        getContentPane().add(tbDomicilio, new org.netbeans.lib.awtextra.AbsoluteConstraints(180, 420, -1, -1));

        btnGroupEntrega.add(tbRecoger);
        tbRecoger.setText("Recoger en Tienda");
        tbRecoger.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tbRecogerActionPerformed(evt);
            }
        });
        getContentPane().add(tbRecoger, new org.netbeans.lib.awtextra.AbsoluteConstraints(410, 420, -1, -1));

        btnAceptar.setText("Aceptar");
        btnAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAceptarActionPerformed(evt);
            }
        });
        getContentPane().add(btnAceptar, new org.netbeans.lib.awtextra.AbsoluteConstraints(270, 470, -1, -1));

        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });
        getContentPane().add(btnCancelar, new org.netbeans.lib.awtextra.AbsoluteConstraints(390, 470, -1, -1));

        cmbCliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbClienteActionPerformed(evt);
            }
        });
        getContentPane().add(cmbCliente, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 80, 290, -1));

        lbCliente.setFont(new java.awt.Font("Tahoma", 1, 11));
        lbCliente.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lbCliente.setText("Cliente");
        getContentPane().add(lbCliente, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 80, 80, 20));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        // TODO add your handling code here:
        JOptionPane.showMessageDialog(this, "La compra ha sido cancelada");
        this.setVisible(false);
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void btnAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAceptarActionPerformed
        // TODO add your handling code here:
        Object obj = cmbCliente.getSelectedItem();
        Cliente c = (Cliente)obj;
        compra.setCliente(c);

        if(!compra.getListaProductos().isEmpty()){
            //if(compra.getfPago() != null){
                if(compra.getfRecogida() != null && compra.getfPago() !=null){
                            int opcion = JOptionPane.showConfirmDialog(this, "¿Desea realizar la compra?");

                            if(opcion == 0){
                                String cadena = compra.getCliente()+"\n";

                                Iterator it = compra.getListaProductos().iterator();
                                cadena += "Productos\n";
                                while(it.hasNext()){
                                    String producto = (String)it.next();
                                    cadena += "         "+producto+"\n";
                                }
                                cadena += "Forma de pago: "+compra.getfPago()+"\n";
                                cadena += "Forma de entrega: "+compra.getfRecogida()+"\n\n";
                                cadena += "Su compra se ha realizado correctamente";
                                JOptionPane.showMessageDialog(this, cadena);
                                this.setVisible(false);
                            }
                            else{

                            }
                }else{
                    String cad = "";

                    if(compra.getfPago() ==null){
                        cad += "No ha seleccionado la forma de pago\n";
                    }
                    if(compra.getfRecogida() == null){
                        cad += "No ha seleccionado el tipo de entrega\n";
                    }
                    JOptionPane.showMessageDialog(this, cad);
                }
        }else{
            JOptionPane.showConfirmDialog(this, "No ha seleccionado ningún producto");
        }
    }//GEN-LAST:event_btnAceptarActionPerformed

    private void chkB1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chkB1ActionPerformed
        // TODO add your handling code here:
        if(this.chkB1.isSelected()){//Añado producto a la lista de compra
            String p = this.chkB1.getText();
            compra.getListaProductos().add(p);
        }
        else{//Elimino producto de la lista de compra
            compra.getListaProductos().remove(this.chkB1.getText());
        }
    }//GEN-LAST:event_chkB1ActionPerformed

    private void chkB6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chkB6ActionPerformed
        // TODO add your handling code here:
        if(this.chkB6.isSelected()){//Añado producto a la lista de compra
            String p = this.chkB6.getText();
            compra.getListaProductos().add(p);
        }
        else{//Elimino producto de la lista de compra
            compra.getListaProductos().remove(this.chkB6.getText());
        }
    }//GEN-LAST:event_chkB6ActionPerformed

    private void chkB2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chkB2ActionPerformed
        // TODO add your handling code here:
        if(this.chkB2.isSelected()){//Añado producto a la lista de compra
            String p = this.chkB2.getText();
            compra.getListaProductos().add(p);
        }
        else{//Elimino producto de la lista de compra
            compra.getListaProductos().remove(this.chkB2.getText());
        }
    }//GEN-LAST:event_chkB2ActionPerformed

    private void chkB3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chkB3ActionPerformed
        // TODO add your handling code here:
        if(this.chkB3.isSelected()){//Añado producto a la lista de compra
            String p = this.chkB3.getText();
            compra.getListaProductos().add(p);
        }
        else{//Elimino producto de la lista de compra
            compra.getListaProductos().remove(this.chkB3.getText());
        }
    }//GEN-LAST:event_chkB3ActionPerformed

    private void chkB4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chkB4ActionPerformed
        // TODO add your handling code here:
        if(this.chkB4.isSelected()){//Añado producto a la lista de compra
            String p = this.chkB4.getText();
            compra.getListaProductos().add(p);
        }
        else{//Elimino producto de la lista de compra
            compra.getListaProductos().remove(this.chkB4.getText());
        }
    }//GEN-LAST:event_chkB4ActionPerformed

    private void chkB5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chkB5ActionPerformed
        // TODO add your handling code here:
        if(this.chkB5.isSelected()){//Añado producto a la lista de compra
            String p = this.chkB5.getText();
            compra.getListaProductos().add(p);
        }
        else{//Elimino producto de la lista de compra
            compra.getListaProductos().remove(this.chkB5.getText());
        }
    }//GEN-LAST:event_chkB5ActionPerformed

    private void chkB7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chkB7ActionPerformed
        // TODO add your handling code here:
        if(this.chkB7.isSelected()){//Añado producto a la lista de compra
            String p = this.chkB7.getText();
            compra.getListaProductos().add(p);
        }
        else{//Elimino producto de la lista de compra
            compra.getListaProductos().remove(this.chkB7.getText());
        }
    }//GEN-LAST:event_chkB7ActionPerformed

    private void chkB8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chkB8ActionPerformed
        // TODO add your handling code here:
        if(this.chkB8.isSelected()){//Añado producto a la lista de compra
            String p = this.chkB8.getText();
            compra.getListaProductos().add(p);
        }
        else{//Elimino producto de la lista de compra
            compra.getListaProductos().remove(this.chkB8.getText());
        }
    }//GEN-LAST:event_chkB8ActionPerformed

    private void chkB9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chkB9ActionPerformed
        // TODO add your handling code here:
        if(this.chkB9.isSelected()){//Añado producto a la lista de compra
            String p = this.chkB9.getText();
            compra.getListaProductos().add(p);
        }
        else{//Elimino producto de la lista de compra
            compra.getListaProductos().remove(this.chkB9.getText());
        }
    }//GEN-LAST:event_chkB9ActionPerformed

    private void chkB10ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chkB10ActionPerformed
        // TODO add your handling code here:
        if(this.chkB10.isSelected()){//Añado producto a la lista de compra
            String p = this.chkB10.getText();
            compra.getListaProductos().add(p);
        }
        else{//Elimino producto de la lista de compra
            compra.getListaProductos().remove(this.chkB10.getText());
        }
    }//GEN-LAST:event_chkB10ActionPerformed

    private void rbEfectivoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbEfectivoActionPerformed
        // TODO add your handling code here:
        compra.setfPago(this.rbEfectivo.getText());
    }//GEN-LAST:event_rbEfectivoActionPerformed

    private void rbTarjetaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbTarjetaActionPerformed
        // TODO add your handling code here:
        compra.setfPago(this.rbTarjeta.getText());
    }//GEN-LAST:event_rbTarjetaActionPerformed

    private void rbTransferenciaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbTransferenciaActionPerformed
        // TODO add your handling code here:
        compra.setfPago(this.rbTarjeta.getText());
    }//GEN-LAST:event_rbTransferenciaActionPerformed

    private void tbDomicilioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tbDomicilioActionPerformed
        // TODO add your handling code here:
        compra.setfRecogida(this.tbDomicilio.getText());
    }//GEN-LAST:event_tbDomicilioActionPerformed

    private void tbRecogerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tbRecogerActionPerformed
        // TODO add your handling code here:
        compra.setfRecogida(this.tbRecoger.getText());
    }//GEN-LAST:event_tbRecogerActionPerformed

    private void cmbClienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbClienteActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cmbClienteActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void inicio() {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new frmCompra().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAceptar;
    private javax.swing.JButton btnCancelar;
    private javax.swing.ButtonGroup btnGroupEntrega;
    private javax.swing.ButtonGroup btnGroupPago;
    private javax.swing.JCheckBox chkB1;
    private javax.swing.JCheckBox chkB10;
    private javax.swing.JCheckBox chkB2;
    private javax.swing.JCheckBox chkB3;
    private javax.swing.JCheckBox chkB4;
    private javax.swing.JCheckBox chkB5;
    private javax.swing.JCheckBox chkB6;
    private javax.swing.JCheckBox chkB7;
    private javax.swing.JCheckBox chkB8;
    private javax.swing.JCheckBox chkB9;
    private javax.swing.JComboBox cmbCliente;
    private javax.swing.JLabel lbCliente;
    private javax.swing.JLabel lbComprar;
    private javax.swing.JLabel lbSeleccionEntrega;
    private javax.swing.JLabel lbSeleccionPago;
    private javax.swing.JLabel lbSeleccionProducto;
    private javax.swing.JRadioButton rbEfectivo;
    private javax.swing.JRadioButton rbTarjeta;
    private javax.swing.JRadioButton rbTransferencia;
    private javax.swing.JToggleButton tbDomicilio;
    private javax.swing.JToggleButton tbRecoger;
    // End of variables declaration//GEN-END:variables

}
